import 'dart:ui' as ui;

import 'package:flutter/material.dart';

import '../base/axis_entity.dart';
import 'added_line.dart';

/// 折线图点数据
///
/// Created by wanggaowan on 2023/9/15 15:43
class LineEntity extends AxisEntity {
  LineEntity(
      {super.xIndex,
      super.value,
      super.isDraw,
      super.data,
      this.xIndexOffset = 0,
      this.drawLabel = false,
      this.label,
      this.labelTextSize = 12,
      this.labelGravity = LabelGravity.center,
      this.labelColor = Colors.black,
      this.drawIndicator = false,
      this.indicatorColor = Colors.black,
      this.indicatorDrawable,
      this.indicatorSize = 5,
      this.labelOffset = 0,
      this.lineStyle = LineStyle.solidLine});

  /// 数据点绘制位置相比[xIndex]偏移百分比，1：往右偏移一个网格宽度，-1：往左偏移一个网格宽度
  final double xIndexOffset;

  /// 是否绘制标签
  final bool drawLabel;

  /// 标签值
  final String? label;

  /// 标签字体大小
  final double labelTextSize;

  /// 标签绘制位置
  final LabelGravity labelGravity;

  /// 标签字体颜色
  final Color labelColor;

  /// 是否绘制指示器
  final bool drawIndicator;

  /// 指示器颜色，如果设置了此值也设置了[indicatorDrawable],则优先使用[indicatorDrawable]。
  final Color indicatorColor;

  /// 指示器图标
  final ui.Image? indicatorDrawable;

  /// 指示器大小
  final double indicatorSize;

  /// 标签离[value]绘制位置的偏移距离
  final double labelOffset;

  /// 绘制线条类型
  final LineStyle lineStyle;
}

/// 折线的样式
enum LineStyle {
  /// 虚线
  dottedLine,

  /// 实线
  solidLine
}

/// 标签基于[LineEntity]绘制坐标的中心点
enum LabelGravity {
  /// 以value所在坐标位置为起点，从左向右绘制
  start,

  /// 以value所在坐标位置为中点
  center,

  /// 以value所在坐标位置为结束点，从右向左绘制
  end
}

/// 折线图数据
class LineData {
  const LineData(
      {required this.values,
      this.lineType = LineType.liner,
      this.cubicIntensity,
      this.lineWidth = 1,
      this.lineColors = const [Colors.grey],
      this.fillBg = false,
      this.fillBgColors = const [],
      this.fillBgDrawable,
      this.addedLines,
      this.verticalAddedLines,
      this.lineDrawCouldBeyondAxis = false})
      : assert(lineWidth > 0, "line width must than zero"),
        assert(lineColors.length > 0,
            "lineColors at least one color value must be set");

  final List<LineEntity> values;

  final LineType lineType;

  /// 如果[lineType]是[LineStyle.cubicBezier],则此值指定贝塞尔曲线的平滑度,默认0.1
  final double? cubicIntensity;

  /// 绘制折线宽度
  final double lineWidth;

  /// 绘制折线颜色,当指定多个颜色值时，则水平渐变
  final List<Color> lineColors;

  /// 是否填充背景
  final bool fillBg;

  /// 填充的背景颜色，当指定多个颜色值时，则垂直渐变。
  /// 如果设置了此值也设置了[fillBgDrawable],则优先使用[fillBgDrawable]。
  final List<Color> fillBgColors;

  /// 填充的背景图像
  final ui.Image? fillBgDrawable;

  /// 附加线数据
  final List<AddedLine>? addedLines;

  /// 垂直方向的附加线数据
  final List<AddedLine>? verticalAddedLines;

  /// 折线绘制时，是否可以超出轴线。超出的情况只在[lineType] == [LineStyle.cubicBezier]和[LineStyle.horizontalBezier]时发生
  final bool lineDrawCouldBeyondAxis;
}

enum LineType {
  /// 直线
  liner,

  /// 三次贝塞尔曲线
  cubicBezier,

  /// 二次贝塞尔曲线
  horizontalBezier
}
